<?php 
namespace Admin\Controller;
/**
 * 后台用户控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class ShopController extends AdminController {
  public function index(){
        $name       =   I('name');
        $map['status']  =   array('egt',0);
        if(is_numeric($name)){
            $map['id|name']=   array(intval($name),array('like','%'.$name.'%'),'_multi'=>true);
        }else{
            $map['name']    =   array('like', '%'.(string)$name.'%');
        }
        $list   = $this->lists('goods', $map);
        $picture =M('picture')->getField('id,path');
        $cate = M('category')->getField('id,title');
        foreach ($list as $k => $v) {
            $list[$k]['catename']=$cate[$list[$k]['cid']];
            $list[$k]['pic']=$picture[$list[$k]['icon']];
        }
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '商品信息';
        $this->display();
    }
    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['uid'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbiduser':
                $this->forbid('goods', $map );
                break;
            case 'resumeuser':
                $this->resume('goods', $map );
                break;
            case 'deleteuser':
                $this->delete('goods', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }
    public function add(){
        if (IS_POST) {
            $add=D('Shop');
            $res=$add->create();
        // $upload = new \Think\Upload();//实例化上传类
        // $upload->exts =array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        // $upload->rootPath ='./Uploads/Picture/'; // 设置附件上传目录
        // $info = $upload->uploadOne($_FILES['pic']);
        // if(!$info) {
        //     $this->error($upload->getError());    
        //  }else{
        //     $_POST['pic']=$info['savepath'].$info['savename'];
        // }
            if ($res) {
                if ($add->add()) {
                    // echo $add->getlastsql();die;

                    $this->success('添加成功',U('index'));
                }else{
                    $this->error('添加失败',U('add'));
                }
            }else{
                $this->error($add->getError());
            }
        }else{
        $category=M('category')->where(array('pid'=>0))->select();
        $this->assign('cate',$category);
        $this->display(); 
        }
        
    }

    public function child(){
        $id=I('post.id');
        $cate=M('category')->where(array('pid'=>$id))->select();
        if ($cate) {
            $this->assign('cate',$cate);
            $res['info']=$this->fetch('child');
            $res['status']=1;
            $res['msg']='success';
        }else{
            $res['info']='';
            $res['status']=0;
            $res['msg']='没有下一级'; 
        }
         $this->ajaxReturn($res);
    }
    public function edit(){

        if(IS_POST){
            var_dump(I('post.'));die;
            $goods = D('goods');
            $data = $goods->create();
            if($data){
                if($goods->save()!== false){
                    $this->success('更新成功',U('index'));
                } else {
                    $this->error('更新失败',U('edit'));
                }
            } else {
                $this->error($goods->getError());
            }
       }else{
        $id = I('get.id');
        $res = M('category')->where(array('pid'=>0))->field('id,title')->select();
        $edit = M('goods')->where(array('id'=>$id))->find();
        $cate = M('category')->where(array('id'=>$edit['cid']))->field('id,pid,title')->find();
        $cateid = M('category')->where(array('id'=>$cate['pid']))->field('id,pid,title')->find();
        $catepid = M('category')->where(array('id'=>$cateid['pid']))->field('id,pid,title')->find();
        // var_dump($cate,$cateid,$catepid);
        // var_dump($res);
        $this->assign('edit',$edit);
        $this->assign('res',$res);
        $this->assign('cate',$cate);
        $this->assign('cateid',$cateid);
        $this->assign('catepid',$catepid);
        $this->meta_title = '编辑商品';
        $this->display();
       }       
    }
}

 ?>